package com.sc.test.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sc.test.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao
 * Created by 2019-01-15
 */
public class CameraActivity extends AppCompatActivity {
    private static final int REQUEST_CAMERA = 100;
    public static String base64Result;
    @BindView(R.id.image)
    ImageView image;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_camera1, R.id.bt_camera2, R.id.bt_camera3})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.bt_camera1:
                intent = new Intent(this, CameraPicActivity.class);
                startActivityForResult(intent, REQUEST_CAMERA);
                break;
            case R.id.bt_camera2:
                intent = new Intent(this, CameraPicActivity1.class);
                startActivityForResult(intent, REQUEST_CAMERA);
                break;
            case R.id.bt_camera3:
                intent = new Intent(this, CameraTestActivity.class);
                startActivityForResult(intent, REQUEST_CAMERA);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {
                byte[] bytes = Base64.decode(base64Result, Base64.NO_WRAP);
                Bitmap img = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                image.setImageBitmap(img);
            }
        }
    }
}
